<template>
  <div id="questions">
    <a-form :model="page" layout="inline">
      <a-form-item field="title" label="题目名称" style="min-width: 300px">
        <a-input v-model="page.title" placeholder="请输入标题名称" />
      </a-form-item>
      <a-form-item field="tags" label="标签" style="min-width: 300px">
        <a-input-tag v-model="page.tags" placeholder="请输入标签" />
      </a-form-item>
      <a-form-item>
        <a-button @click="doSearch">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0"></a-divider>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        total: total,
        pageSize: page.pageSize,
        current: page.current,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #accept="{ record }">
        {{
          `${
            record.submitNum != 0 ? record.acceptedNum / record.submitNum : "0"
          }%(${record.acceptedNum}/${record.submitNum})`
        }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doQuestion(record)"
            >去做题
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { Question, QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const router = useRouter();
const dataList = ref();
const page = ref({
  title: "",
  tags: [],
  current: 1,
  pageSize: 10,
  sortField: "createTime",
});
const total = ref(0);
const searchQuestion = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    page.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败," + res.message);
  }
};
onMounted(() => {
  searchQuestion();
});

//相当于监听searchQuestion中的所有变量,当其中任何一个变量改变的时候就会执行
watchEffect(() => {
  searchQuestion();
});

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "题目名称",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过次数",
    slotName: "accept",
  },

  {
    title: "更新时间",
    dataIndex: "updateTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const onPageChange = (pageCurrent: number) => {
  page.value = {
    ...page.value,
    current: pageCurrent,
  };
};

//跳转到做题页面
const doQuestion = (record: Question) => {
  router.push({
    path: "/doQuestion",
    query: {
      id: record.id,
    },
  });
};

//进行搜索
const doSearch = () => {
  page.value = {
    ...page.value,
    current: 1, //将当前页设置为1
  };
};
</script>

<style>
#questions {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
